//OpenCV
#include "cv.h"
#include "highgui.h"
//others
#include <string>
#include <stdio.h>
#include "InformationPresentation.h"

  IplImage * work_image_rgb = 0;
  void
  show_matrix(int ** r, int ** g, int ** b, int sx, int sy)
  {
    if (work_image_rgb != 0)
      cvReleaseImage(&work_image_rgb);
    work_image_rgb = cvCreateImage(cvSize(sx, sy), IPL_DEPTH_8U, 3);
    for (int i = 0; i < sx; i++)
      {
        for (int j = 0; j < sy; j++)
          {
            if (b[i][j] != -123)
              {
                int valb = b[i][j];
                work_image_rgb->imageData[3 * (j * sx + i) + 0] = valb;

              }
            else
              {
                work_image_rgb->imageData[3 * (j * sx + i) + 0] = 0;
              }

            if (g[i][j] != -123)
              {
                int valg = g[i][j];
                work_image_rgb->imageData[3 * (j * sx + i) + 1] = valg;

              }
            else
              {
                work_image_rgb->imageData[3 * (j * sx + i) + 1] = 0;
              }

            if (r[i][j] != -123)
              {
                int valr = r[i][j];
                work_image_rgb->imageData[3 * (j * sx + i) + 2] = valr;

              }
            else
              {
                work_image_rgb->imageData[3 * (j * sx + i) + 2] = 0;
              }
          }
      }
    cvNamedWindow("rgb image", CV_WINDOW_AUTOSIZE);
    cvShowImage("rgb image", work_image_rgb);
}


  //IplImage * work_image_rgb = 0;
  void
  show_matrix(double ** r, double ** g, double ** b, int sx, int sy)
  {
    double min = 10000000;
    double max = -1000000;
    for (int i = 0; i < sx; i++)
      {
        for (int j = 0; j < sy; j++)
          {
            if (r[i][j] != -123)
              {
                if (r[i][j] < min)
                  {
                    min = r[i][j];
                  }
                else if (r[i][j] > max)
                  {
                    max = r[i][j];
                  }
              }
            if (g[i][j] != -123)
              {
                if (g[i][j] < min)
                  {
                    min = g[i][j];
                  }
                else if (g[i][j] > max)
                  {
                    max = g[i][j];
                  }
              }

            if (b[i][j] != -123)
              {
                if (b[i][j] < min)
                  {
                    min = b[i][j];
                  }
                else if (b[i][j] > max)
                  {
                    max = b[i][j];
                  }
              }
          }
      }
    double mul = 255.0f / (max - min);
    if (work_image_rgb != 0)
      cvReleaseImage(&work_image_rgb);
    work_image_rgb = cvCreateImage(cvSize(sx, sy), IPL_DEPTH_8U, 3);
    for (int i = 0; i < sx; i++)
      {
        for (int j = 0; j < sy; j++)
          {
            if (b[i][j] != -123)
              {
                int valb = (b[i][j] - min) * mul;
                work_image_rgb->imageData[3 * (j * sx + i) + 0] = valb;

              }
            else
              {
                work_image_rgb->imageData[3 * (j * sx + i) + 0] = 0;
              }

            if (g[i][j] != -123)
              {
                int valg = (g[i][j] - min) * mul;
                work_image_rgb->imageData[3 * (j * sx + i) + 1] = valg;

              }
            else
              {
                work_image_rgb->imageData[3 * (j * sx + i) + 1] = 0;
              }

            if (r[i][j] != -123)
              {
                int valr = (r[i][j] - min) * mul;
                work_image_rgb->imageData[3 * (j * sx + i) + 2] = valr;

              }
            else
              {
                work_image_rgb->imageData[3 * (j * sx + i) + 2] = 0;
              }
          }
      }
    cvNamedWindow("rgb image", CV_WINDOW_AUTOSIZE);
    cvShowImage("rgb image", work_image_rgb);
}

  void
  show_matrix(bool ** boolmat, int sx, int sy)
  {
    if (work_image_rgb != 0)
      cvReleaseImage(&work_image_rgb);
    work_image_rgb = cvCreateImage(cvSize(sx, sy), IPL_DEPTH_8U, 3);
    for (int i = 0; i < sx; i++)
      {
        for (int j = 0; j < sy; j++)
          {
            if (boolmat[i][j])
              {
                work_image_rgb->imageData[3 * (j * sx + i) + 0] = 255;
                work_image_rgb->imageData[3 * (j * sx + i) + 1] = 255;
                work_image_rgb->imageData[3 * (j * sx + i) + 2] = 255;
              }
              else{
                work_image_rgb->imageData[3 * (j * sx + i) + 0] = 0;
                work_image_rgb->imageData[3 * (j * sx + i) + 1] = 0;
                work_image_rgb->imageData[3 * (j * sx + i) + 2] = 0;
              }
          }
      }
    cvNamedWindow("bool mat", CV_WINDOW_AUTOSIZE);
    cvShowImage("bool mat", work_image_rgb);
}
